package Ui;

import java.awt.Color;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import Messaging.Notification;
import Messaging.NotificationManager;
import Model.Player;

/*
 * @author Roch San Buenaventura
 */

public class PlayerPanel extends JPanel 
{

	private static final long serialVersionUID = 80;

	Dimension screen_ = Toolkit.getDefaultToolkit().getScreenSize();
	double width_ = screen_.getWidth() - DisplayAssembler.getRightEdge();
	int height_ = screen_.height;
	private JTabbedPane infoPanel_;
	private JPanel panel_[];
	private JLabel properties_, cashLabel_;
	private JLabel cashAmount_[];
	private JScrollPane scrollPane_[];
	private JList list_[];
	private JLabel FreeOutJail[];
	Color background_;
	ImageIcon enabled_, disabled_, getOutOfJail;

	ArrayList<Player> players_;

	Font propertyFont_ = new Font("SansSerif", Font.PLAIN, 16);

	public PlayerPanel(ArrayList<Player> players) {

		players_ = players;
		background_ = new Color(239, 227, 160);
		enabled_ = new ImageIcon("MonopolyImages/Logos/head.png");
		disabled_ = new ImageIcon("MonopolyImages/Logos/jailhead.png");
		getOutOfJail = new ImageIcon("MonopolyImages/Logos/jailout.png");

		Font nameFont = new Font("SansSerif", Font.BOLD, 18);

		this.setSize(250, 400);
		this.setLayout(null);

		infoPanel_ = new JTabbedPane();
		infoPanel_.setBounds(0, 0, 250, 400);
		infoPanel_.setFont(nameFont);

		cashAmount_ = new JLabel[players.size()];
		FreeOutJail = new JLabel[players.size()];
		list_ = new JList[players.size()];
		scrollPane_ = new JScrollPane[players.size()];
		panel_ = new JPanel[players.size()];

		for (int i = 0; i < players.size(); i++) {
			cashAmount_[i] = new JLabel("");
			FreeOutJail[i] = new JLabel(getOutOfJail);

			list_[i] = new JList(players.get(i).getProperties().toArray());
			scrollPane_[i] = new JScrollPane(list_[i]);

			list_[i].setBackground(new Color(240, 240, 240));
			list_[i].setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			list_[i].setSelectionBackground(new Color(250, 250, 250));
			list_[i].setVisibleRowCount(16);
			list_[i].setFont(propertyFont_);

			scrollPane_[i].setBackground(new Color(240, 240, 240));
			scrollPane_[i].setBounds(0, 90, 250, 350);
			scrollPane_[i].setFont(propertyFont_);
		}


		panel_[0] = createPanel(players_.get(0), cashAmount_[0], FreeOutJail[0], scrollPane_[0], list_[0]);
		infoPanel_.setIconAt(0, enabled_);

		if (players_.size() > 1)
		{
		panel_[1] = createPanel(players_.get(1), cashAmount_[1], FreeOutJail[1], scrollPane_[1], list_[1]);
		infoPanel_.setIconAt(1, disabled_);

			if (players_.size() > 2) 
			{
				panel_[2] = createPanel(players_.get(2), cashAmount_[2], FreeOutJail[2], scrollPane_[2], list_[2]);
				infoPanel_.setIconAt(2, disabled_);
			
				if (players_.size() > 3) 
				{
					panel_[3] = createPanel(players_.get(3), cashAmount_[3], FreeOutJail[3], scrollPane_[3], list_[3]);
					infoPanel_.setIconAt(3, disabled_);
					
					if (players_.size() > 4) 
					{
						panel_[4] = createPanel(players_.get(4), cashAmount_[4], FreeOutJail[4], scrollPane_[4], list_[4]);
						infoPanel_.setIconAt(4, disabled_);
					
						if (players_.size() > 5) 
						{
							panel_[5] = createPanel(players_.get(5), cashAmount_[5], FreeOutJail[5], scrollPane_[5], list_[5]);
							infoPanel_.setIconAt(5, disabled_);
						
							if (players_.size() > 6) 
							{
								panel_[6] = createPanel(players_.get(6), cashAmount_[6], FreeOutJail[6], scrollPane_[6], list_[6]);
								infoPanel_.setIconAt(6, disabled_);
							
								if (players_.size() == 8) 
								{
									panel_[7] = createPanel(players_.get(7), cashAmount_[7], FreeOutJail[7], scrollPane_[7], list_[7]);
									infoPanel_.setIconAt(7, disabled_);
								}
							}
						}
					}
				}
			}
		}

		Point location = new Point(700, 25);
		DisplayAssembler.getInstance().addComponent(this, location, JLayeredPane.PALETTE_LAYER);


		infoPanel_.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		this.add(infoPanel_);
		this.setVisible(true);

		NotificationManager.getInstance().addObserver(Notification.UPDATE_PROPERTIES, this, "updateProperties");
		NotificationManager.getInstance().addObserver(Notification.UPDATE_CASH, this, "updateCash");
		NotificationManager.getInstance().addObserver(Notification.END_TURN, this, "switchPanel");
		NotificationManager.getInstance().addObserver(Notification.USED_JAIL_CARD, this, "usedCard");
		NotificationManager.getInstance().addObserver(Notification.GAINED_JAIL_CARD, this, "gainedCard");
	}

	private JPanel createPanel(Player player, JLabel cashAmount, JLabel getOffAcProFree, JScrollPane scroll, JList list) {
		JPanel panel = new JPanel();
		Font labelFont = new Font("SansSerif", Font.BOLD, 20);
		Font cashFont = new Font("SansSerif", Font.BOLD, 35);

		String cash = "" + player.getCash();

		panel.setLayout(null);

		cashLabel_ = new JLabel("Cash: $ ");
		cashLabel_.setFont(labelFont);
		cashLabel_.setBounds(5, 0, 200, 50);

		cashAmount.setText(cash);
		cashAmount.setFont(cashFont);
		cashAmount.setBounds(75, 0, 250, 50);

		getOffAcProFree.setBounds( 175, 0, 80, 115);
		getOffAcProFree.setEnabled(true);
		getOffAcProFree.setVisible(false);

		properties_ = new JLabel("Properties Owned: ");
		properties_.setFont(labelFont);
		properties_.setBounds(5, 35, 250, 50);

		scroll.setVisible(true);
		list.setVisible(true);
		UIManager.put("TabbedPane.selected", new Color(207, 236, 236));
		UIManager.put("TabbedPane.focus", new Color(207, 236, 236));

		panel.add(cashLabel_);
		panel.add(cashAmount);
		panel.add(getOffAcProFree);
		panel.add(properties_);
		panel.add(scroll);
		panel.add(list);

		infoPanel_.addTab(player.getName(), panel);
		return panel;
	}

	public void updateCash(final Object object) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					Player player = (Player) object;
					String cash = "" + player.getCash();
		
					cashAmount_[player.getIndex()].setText(cash);
		
				} catch (ClassCastException e) {
					System.err.println("Unexpected object passed to updateCash");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void updateProperties(final Object object) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					Player player = (Player) object;
					int i = player.getIndex();

					list_[i].setListData(player.getPropertyArray());
					list_[i].setVisibleRowCount(16);
					list_[i].setFont(propertyFont_);

					scrollPane_[i].setViewportView(list_[i]);
					scrollPane_[i].setBackground(new Color(240, 240, 240));
					scrollPane_[i].setBounds(0, 110, 250, 350);
					scrollPane_[i].setVisible(true);
					scrollPane_[i].setFont(propertyFont_);

					panel_[i].add(scrollPane_[i]);
				} catch (ClassCastException e) {
					System.err.println("Unexpected object passed to updateCash");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public void gainedCard(Object object) {
		try {
			Player player = (Player) object;
			int i = player.getIndex();

			FreeOutJail[i].setVisible(true);
			FreeOutJail[i].setEnabled(true);
		} catch (ClassCastException e) {
			System.err.println("Unexpected object passed to updateCard");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void usedCard(Object object) {
		try {
			Player player = (Player) object;
			int i = player.getIndex();

			FreeOutJail[i].setVisible(false);
		} catch (ClassCastException e) {
			System.err.println("Unexpected object passed to updateCard");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void removePanel(Object i) {
		infoPanel_.remove((Integer) i);
	}
	
	public void switchPanel(final Object i) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					Integer number = (Integer) i;
					for (int j = 0; j < players_.size(); ++j) {
						if (number == j) {
							infoPanel_.setSelectedIndex((int)number);
							infoPanel_.setIconAt(number, enabled_);
						}
						else {
							infoPanel_.setIconAt(j, disabled_);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
